// src/pages/UserList/components/SysUserAgentModal.tsx
import React, { useState, useEffect } from 'react';
import { Modal, Form, Input, DatePicker, Switch, message } from 'antd';
import { getUserInfo } from '@/services/user'; // 假设需要获取用户信息

type SysUserAgentModalProps = {
    visible: boolean;
    currentUser?: any; // 根据实际类型调整，包含用户代理信息
    onOk: (values: any) => void;
    onCancel: () => void;
};

const SysUserAgentModal: React.FC<SysUserAgentModalProps> = ({
                                                                 visible,
                                                                 currentUser,
                                                                 onOk,
                                                                 onCancel,
                                                             }) => {
    const [form] = Form.useForm();
    const [confirmLoading, setConfirmLoading] = useState(false);

    useEffect(() => {
        if (visible && currentUser) {
            // 加载代理设置
            form.setFieldsValue({
                userId: currentUser.id, // 代理设置关联的用户ID
                agentId: currentUser.agentId, // 假设用户对象中有 agentId
                startTime: currentUser.startTime, // 假设有开始时间
                endTime: currentUser.endTime, // 假设有结束时间
                status: currentUser.agentStatus, // 假设有代理状态
            });
        } else if (visible && !currentUser) {
            form.resetFields();
        }
    }, [visible, currentUser, form]);

    const handleOk = async () => {
        try {
            const values = await form.validateFields();
            setConfirmLoading(true);

            // 这里需要调用实际的代理设置 API
            // 假设有一个 setAgent API
            // const response = await setAgent(values);
            console.log('提交代理设置:', values);
            // if (response.success) {
            message.success('代理设置成功');
            onOk(values);
            // } else {
            //   message.error(response.message || '代理设置失败');
            // }
        } catch (error) {
            console.error('提交代理设置失败:', error);
            // message.error('表单验证失败或提交出错');
        } finally {
            setConfirmLoading(false);
        }
    };

    return (
        <Modal
            title="用户代理设置"
            open={visible}
            onOk={handleOk}
            onCancel={onCancel}
            confirmLoading={confirmLoading}
            maskClosable={false}
            width={600}
        >
            <Form
                form={form}
                layout="vertical"
            >
                <Form.Item
                    name="userId"
                    label="用户ID"
                    hidden // 通常隐藏，用于提交
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="agentId"
                    label="代理人ID"
                    rules={[{ required: true, message: '请输入代理人ID!' }]}
                >
                    <Input placeholder="请输入代理人ID" />
                </Form.Item>
                <Form.Item
                    name="startTime"
                    label="代理开始时间"
                    rules={[{ required: true, message: '请选择开始时间!' }]}
                >
                    <DatePicker showTime style={{ width: '100%' }} />
                </Form.Item>
                <Form.Item
                    name="endTime"
                    label="代理结束时间"
                    rules={[{ required: true, message: '请选择结束时间!' }]}
                >
                    <DatePicker showTime style={{ width: '100%' }} />
                </Form.Item>
                <Form.Item
                    name="status"
                    label="代理状态"
                    valuePropName="checked"
                >
                    <Switch checkedChildren="启用" unCheckedChildren="禁用" />
                </Form.Item>
            </Form>
        </Modal>
    );
};

export default SysUserAgentModal;